import 'package:cms/api/term.dart';
import 'package:cms/models/term.dart';
import 'package:common/dio/model.dart';
import 'package:common/export/bloc.dart';
import 'package:common/utils/lara_press_verify.dart';
import 'term_list_state.dart';

class TermListCubit extends Cubit<TermListState> {
  final String searchKeyword;
  TermListCubit({this.searchKeyword = ''}) : super(TermListState.initial()) {
    getTerms();
  }

  Future<void> getTerms({bool isRefresh = false}) async {
    try {
      Map<String, dynamic> params = {};
      if (!LaraPressVerify.isEmpty(searchKeyword)) {
        params['search'] = searchKeyword;
      }
      LarapressListResponse<WpTerm> response = await LaraPressTermApi()
          .getTerms(params);
      emit(state.copyWith(list: response.data));
      if (isRefresh) {
        state.refreshController.refreshCompleted();
      }
    } catch (e) {}
  }
}
